package shell;

import games.TicTacToeState;
import games.TicTacToeState.TicTacToeMove;

public class TicTacToeGame extends AbstractGame<TicTacToeMove, TicTacToeState> {
	
	public TicTacToeGame(String[] args) {
		super(TicTacToeState.class, 10, args);
	}

	@Override
	protected TicTacToeState createState(String name) {
		return new TicTacToeState();
	}

	@Override
	protected void displayGUI(TicTacToeState game) {
		;
	}
	
	@Override
	protected String render(TicTacToeState game) {
		StringBuilder buffer = new StringBuilder();
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				Boolean cell = game.cell(i, j);
				char c = cell == null ? ' ' : cell ? '0' : 'X';
				buffer.append(c);
				if (j < 2) {
					buffer.append('|');
				}
			}
			if (i < 2) {
				buffer.append("\n------\n");
			}
		}
		return buffer.toString();
	}

	@Override
	protected TicTacToeMove parseMove(TicTacToeState state, String command) {
		TicTacToeMove result;
		try {
			int cell = Integer.parseInt(command);
			if (0 <= cell & cell < 9) {
				return TicTacToeMove.valueOf(state.player(), cell);
			} else {
				result = null;
			}
		} catch (NumberFormatException e) {
			result = null;
		}
		return result;
	}

	public static void main(String[] args) {
		new TicTacToeGame(args).play();
	}

	@Override
	protected TicTacToeMove waitForMove(TicTacToeState state) {
		return null;
	}

}
